//
//  SearchViewController.m
//  CarFace
//
//  Created by dlios on 15/3/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SearchViewController.h"
#import "CarCategory.h"
#import "BrandFindTableViewCell.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;


@end

@implementation SearchViewController

- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [super dealloc];
    
}

//处理本地数据
- (void)handleData
{
    self.arr = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Test" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//    NSLog(@"%@", [result valueForKey:@"message"]);
    
    NSMutableArray *array = [[result valueForKey:@"result"]valueForKey:@"brandlist"];
    for (NSDictionary *dic in array) {
        CarCategory *info = [[CarCategory alloc]initWithDictionary:dic];
//        NSLog(@"%@",info.letter);
        
        [self.arr addObject:info];
        [self.tableView reloadData];
        [info release];
        
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor purpleColor];
    self.tableView.dataSource = self;
    self.tableView.delegate =self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self handleData];
    

    
}

//每个分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//   return  3;
    
    CarCategory *info = [self.arr objectAtIndex:section];
    NSArray *array = [info valueForKey:@"list"];
//    NSLog(@"%ld",array.count);
    return array.count;
    
    
}


//cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"ff";
    BrandFindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[BrandFindTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    
    CarCategory *info = [self.arr objectAtIndex:indexPath.row];
    cell.info = info;
    NSLog(@"%@",info.name);
    return cell;
    
    
}


//设置分区数量

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arr.count;
}


//设置侧边栏各区名称
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < self.arr.count; i++) {
        CarCategory *carinfo = [self.arr objectAtIndex:i];
        NSString *str = carinfo.letter;
//        NSLog(@"%@",str);
        [array addObject:str];

    }
//    NSLog(@"%@",array[1]);
    return array;
}

//设置分区顶部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}


//设置各个分区顶部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CarCategory *info = [self.arr objectAtIndex:section];
    NSString *str = info.letter;
    return str;
    
}



//设置各个分区顶部视图风格
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UILabel *label = [[UILabel alloc]init];
//    label.backgroundColor = [UIColor greenColor];
//    label.textAlignment = NSTextAlignmentLeft;
//    
//    return [label autorelease];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
